/**
 * 将扁平数据转换成tree data
 * @param {*} data
 */
import {parse, stringify} from "@/assets/js/util";

export const initOrgTreeData = (items, id = '', link = 'parentId') => {
  return items.filter(item => id ? item[link] == id : (item[link] == '0' || item[link] == '' || item[link] == 0 || item[link] == -1)).map(item => ({
    ...item,
    children: initOrgTreeData(items, item.id) || []
  }));
}
/**
 * 删除空数组属性
 * @param {*} item 
 */
export const deleteEmptyChildren = (item) => {
    for (let i of item) {
        if (i.children) {
            if (i.children && i.children.length === 0) {
                delete(i.children)
            } else {
                deleteEmptyChildren(i.children)
            }
        } 
    }
    return item
}

// 处理： A->B->C 现在返回了B以及B下面的数据 但是没有A 导致无法渲染树形
export const getNotRelationList = (list)  => {
    let copy = []
    try {
        copy = parse(stringify(list))
        copy.forEach(element => {
            let index = list.findIndex(cy => cy.id == element.parentId)
            if (index == -1 && element.id != 0 && element.parentId != -1) element.parentId = 0
        })
    } catch(e) {
        copy = []
    }
    return copy
}


/**
 * 改成element 级联支持的属性
 * @param {*} items 
 */
export const genSimpleList = (items) => {
    let list = []
    for (let i of items) {
        let o = {}
        o.label = i.name || i.categoryName || i.title
        o.value = i.id
        o.id = i.id
        o.parentId = i.parentId
        // 增加参数
        for (let k in i) {
            // if (typeof i[k] !== 'object') {
                o[k] = i[k]
            // }
        }

        list.push(o)
    }
    return list
}
/**
 * 删除树的最外层
 * @param list
 * @returns {*[]}
 */
export const deleteFirstTree = (list) => {
    let arr = []
    for (let i of list) {
        i.children && i.children.length > 0 && arr.push(i.children)
    }
    return flatten(arr)
}

/**
 * 数组扁平化
 */
export const  flatten= arr=> {
    return arr.reduce((res, next) => {
        return res.concat(Array.isArray(next) ? flatten(next) : next)
    }, [])
}

/**
 * 改成element 级联支持的属性
 * @param {*} items 
 */
export const genDeepList = (items, n, old) => {
    let list = []
    for (let i of items) {
        let o = {}
        o.label = i.name
        o.value = i.id || i.categoryId
        o.id = i.id || i.categoryId
        o.parentId = i.parentId
        // 增加参数
        for (let k in i) {
            if (Array.isArray(i[k])){
                i[k] = genDeepList(i[k], n, old)
            }
            if (k === old) {
                o[n] = i[k]
            }
            o[k] = i[k]
        }

        list.push(o)
    }
    return list
}

/**
 * 获取子级的所有父级并输出 
 * @param {*} orgId 单位ID
 * @param {*} orgList 单位列表
 * return 返回 ['1','1-1','1-1-1']
 */

export const getOrgParents = (orgId, orgList) => {
    var temp = []
    var closure = function (arr, id) {
        for (var i = 0; i < arr.length; i++) {
            var item = arr[i]
            if (item.orgId === id) {
                temp.push(item.orgId)
                closure(orgList, item.parentId)
                break
            } 
        }
    }
    closure(orgList, orgId)
    return temp.reverse()
}

/**
 * 将结果转换成级联数据
 * @param {*} data // 作品分类
 * @param array 项目分类
 * @param formType: 1 不显示全部作品， formType: 2 不显示分类
 */
export const generateAwardsWorksOptions = (data, array, formType) => {
    let options = formType == 2 ? [{ value: '-1', label: '全部作品' }] : []
    if (formType == 1 && array.length > 0) {
        let markOptions = { value: '-2', label: '分类' }
        markOptions.children = []
        for (let i of array) {
            markOptions.children.push({ value: i.markId + '**', label: i.name })
        }
        options.push(markOptions)
    }
    for (let i of data) {
        let o = { value: i.awardsId, label: i.name },
            c1 = []
        // 如果奖项未开启评审 则默认未空数组，不能为NULL
        i.reviewInfoDtoList = i.reviewInfoDtoList || []
        for (let j of i.reviewInfoDtoList) {
            let o2 = { value: j.reviewId, label: j.name }
            o2.children = [
                { value: j.reviewId + '==3', label: '全部作品（' + j.worksCount + '）' },
                { value: j.reviewId + '==1', label: '通过作品（' + j.passWorksCount + '）' },
                { value: j.reviewId + '==2', label: '未通过作品（' + j.unPassWorksCount + '）' }
            ]
            c1.push(o2)
        }
        o.children = c1
        options.push(o)
    }
    return options

}